﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Forest.Core.Infrastructure;
using Forest.Web.Framework.Mvc;
using Forest.Web.Framework.Mvc.Routes;
using Forest.Web.Framework;
using Forest.Web.Framework.Themes;

namespace Forest.Cat.Web {
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //register custom routes (plugins, etc)
            var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
            routePublisher.RegisterRoutes(routes);

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[]{"Forest.Cat.Web.Controllers"}// 参数默认值
            );

        }

        protected void Application_Start() {

            //initialize engine context
            EngineContext.Initialize(false);

            bool databaseInstalled = true;

            var dependencyResolver = new AppDependencyResolver();
            DependencyResolver.SetResolver(dependencyResolver);


            if (databaseInstalled) {
                //remove all view engines
                ViewEngines.Engines.Clear();
                //except the themeable razor view engine we use
                ViewEngines.Engines.Add(new ThemeableRazorViewEngine());
            }

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

           


            //start scheduled tasks
            //if (databaseInstalled) {
            //    TaskManager.Instance.Initialize();
            //    TaskManager.Instance.Start();
            //}
        }
    }
}